package lvbu.wang.lvbuoperator.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qing.library.utils.L;

import java.util.List;

import lvbu.wang.lvbuoperator.R;
import lvbu.wang.lvbuoperator.bean.JsonSaleProduct;

/**
 * Created by DaiQing.Zha on 2017/3/18 0018.
 */
public class SaleProductAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<JsonSaleProduct.ProductCategoryDict> group;
    private List<List<JsonSaleProduct.ProductInfo>> child;

    public SaleProductAdapter(Context context, List<JsonSaleProduct.ProductCategoryDict> group, List<List<JsonSaleProduct.ProductInfo>> child) {
        this.context = context;
        this.group = group;
        this.child = child;
    }

    @Override
    public int getGroupCount() {
        return group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return child.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_product_category, null);
            holder = new ViewHolderGroup();
            holder.categoryName = (TextView) convertView.findViewById(R.id.tv_categoryName);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolderGroup) convertView.getTag();
        }
        holder.categoryName.setText(group.get(groupPosition).getCategoryName());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderChild holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_product_info, null);
            holder = new ViewHolderChild();
            holder.productName = (TextView) convertView.findViewById(R.id.tv_productName);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolderChild) convertView.getTag();
        }
        holder.productName.setText("* " + child.get(groupPosition).get(childPosition).getProductName());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ViewHolderGroup {
        TextView categoryName;
    }

    class ViewHolderChild {
        TextView productName;
    }
}
